package com.contract.spirit.ui.exchange

import android.app.Activity
import android.content.Intent
import com.contract.commonlib.util.ext.setOnSingleClickListener
import com.contract.commonlib.base.BaseActivity
import com.contract.commonlib.http.ext.parseState
import com.contract.commonlib.util.RecycleviewLayoutManger
import com.contract.spirit.R
import com.contract.spirit.bean.response.MainPageResponse
import com.contract.spirit.databinding.ActivityExchangeBindListBinding
import com.contract.spirit.manage.BindExchangeDialogManager
import com.contract.spirit.ui.UserViewModel
import com.contract.spirit.ui.exchange.adapter.ExchangeBindListAdapter
import com.contract.spirit.ui.main.MainActivity

/**
 * @author: zhengjun
 * created: 2021/9/26
 * desc:
 */
class ExchangeBindListActivity : BaseActivity<ActivityExchangeBindListBinding, UserViewModel>() {
    private val bindListAdapter by lazy {
        ExchangeBindListAdapter()
    }

    override fun setContentView(): Int {
        return R.layout.activity_exchange_bind_list
    }

    override fun initData() {
        RecycleviewLayoutManger.setVerticalManager(mBinding.rvExchange, mContext)
        mBinding.rvExchange.adapter = bindListAdapter;
        bindListAdapter.setOnItemClickListener { _, _, position ->
            val item: MainPageResponse.ExchangePollInfo = bindListAdapter.getItem(position)
            if (!item.isHasBind()) {
                with(BindExchangeDialogManager) { showBindExchangeDialog(mContext, item) }
            }
        }
        mBinding.btnGo.setOnSingleClickListener {
            MainActivity.launch(mContext)
        }
    }

    override fun registerNecessaryObserver() {
        super.registerNecessaryObserver()
        mViewModel.exchangeResponse.observe(this, { it ->
            parseState(it, {
                bindListAdapter.setList(it.exchangeList)
            })
        })
    }

    override fun onReload() {
        mViewModel.exchangeResponse.value = null
        getExchangeList()
    }

    private fun getExchangeList() {
        mViewModel.getExchangeList()
    }

    override fun onResume() {
        super.onResume()
        getExchangeList()
    }

    companion object{
        fun launch(activity: Activity){
            activity.startActivity(Intent(activity,ExchangeBindListActivity::class.java))
        }
    }
}